<?php
class ccdsResponse
    extends xajaxResponse
{
    var $bHeaderSent=false;
    var $fTimeOut=0.00001;
    var $comet=false;

    var $timer=0;
		
		var $openProgress = false;

    var $progressOpen=false;
    var $attack=1000;
    /*
        Function: xajaxCometResponse
        
        calls  parent function xajaxResponse();
    */

    function ccdsResponse( $comet=false, $fTimeOut=false )
    {
        if ( false != $fTimeOut )
            $this->fTimeOut=$fTimeOut;

        if ( 1 == $_REQUEST['ccdscomet'] )
            $comet=true;
        $this->comet=$comet;
        $this->timer=microtime();
        parent::xajaxResponse();
    }

    function printOutput( )
    {
        if ( !$this->comet )
            return parent::printOutput();

        $this->flush( true );

        if ( "HTML5DRAFT" == $_GET['xjxstreaming'] )
        {

            $response="";
            $response.="Event: xjxendstream\n";
            $response.="data: done\n";
            $response.="\n";
            print $response;
            ob_flush();
            flush();
        }
        return;
    }

    /*
        Function: flush_XHR
        
        Flushes the command queue for comet browsers.
    */

    function flush_XHR( )
    {
        if ( !$this->bHeaderSent )
        {
            $this->_sendHeaders();
            $this->bHeaderSent=true;
        }

        ob_start();
        $this->_printResponse_XML();
        $c=ob_get_contents();
        ob_get_clean();
        $c=str_replace( chr( 1 ), " ", $c );
        $c=str_replace( chr( 2 ), " ", $c );
        $c=str_replace( chr( 31 ), " ", $c );
        $c=str_replace( "", " ", $c );

        if ( $c == "<xjx></xjx>" )
            return false;
        print $c;
        ob_flush();
        flush();
    }


    /*
        Function: flush_activeX
        
        Flushes the command queue for ActiveX browsers.
    */

    function flush_activeX( )
    {
        ob_start();
        $this->_printResponse_XML();
        $c=ob_get_contents();
        ob_end_clean();

        $c='<?xml version="1.0" ?>' . $c;
        $c=str_replace( '"', '\"', $c );
        $c=str_replace( "\n", '\n', $c );
        $c=str_replace( "\r", '\r', $c );

        $response="";
        $response.="<script>top.document.callback(\"";
        $response.=$c;
        $response.="\");</script>";

        print $response;
        ob_flush();
        flush();
    //$this->sleep( 0.0001 );
    }

    /*
        Function: flush_HTML5DRAFT
        
        Flushes the command queue for HTML5DRAFT browsers.
    */

    function debug( $msg ) {

    //	file_put_contents("/var/www/web3/html/debug.txt",$msg,FILE_APPEND);

    }

    function flush_HTML5DRAFT( )
    {
        if ( !$this->bHeaderSent )
        {
            header( "Content-Type: application/x-dom-event-stream" );
            $this->bHeaderSent=1;
        }

        ob_start();
        $this->_printResponse_XML();
        $c=ob_get_contents();
        ob_end_clean();
        $c=str_replace( "\n", '', $c );
        $c=str_replace( "\r", '', $c );
        $response="";
        $response.="Event: xjxstream\n";
        $response.="data: $c\n";
        $response.="\n";
        print $response;

        //		$this->debug($response);

        ob_flush();
        flush();
    }


    /*
        Function: flush
        
        Determines which browser is wating for a response and calls the according flush function.
    */
    function flush( $anyway=false )
    {
        if ( !$this->comet )
            return;

        if ( 0 == count( $this->aCommands ) )
            return false;

        if ( false == $anyway )
        {
            $diff=microtime() - $this->timer;

            if ( $diff > $this->attack )
                return;
        }

        if ( "xhr" == $_SERVER['HTTP_STREAMING'] )
        {
            $this->flush_XHR();
        }
        elseif ( "HTML5DRAFT" == $_GET['xjxstreaming'] )
        {
            $this->flush_HTML5DRAFT();
        }
        else
        {
            $this->flush_activeX();
        }
        $this->aCommands=array();
    }

    /*
        Function: sleep
        
        Very accurate sleep function.
    */
    function sleep( $seconds )
    {
        usleep( floor( $seconds * 1000000 ) );
    }

    function loadClassCommand( )
    {

        $iNumargs=func_num_args();
        $aArgList=func_get_args();

        $sClassName=array_shift( $aArgList );
        $sMethod=array_shift( $aArgList );

        $objModuleManager=ccdsModuleManager::getInstance();
        $foo=$objModuleManager->get( $sClassName );

        if ( method_exists( $foo, $sMethod ) )
        {
            $tmp=call_user_func_array( array
                (
                &$foo,
                $sMethod
                ), $aArgList );

            $this->loadcommands( $tmp );
        }
    }


    function loadcommands( $mCommands, $bBefore=false )
    {
        if ( is_a( $mCommands, 'xajaxResponse' ) || is_a( $mCommands, 'ccdsResponse' ) )
        {
            $this->returnValue=$mCommands->returnValue;

            if ( $bBefore )
            {
                $this->aCommands=array_merge( $mCommands->aCommands, $this->aCommands );
            }
            else
            {
                $this->aCommands=array_merge( $this->aCommands, $mCommands->aCommands );
            }
        }
        else if ( is_array( $mCommands ) )
        {
            if ( $bBefore )
            {
                $this->aCommands=array_merge( $mCommands, $this->aCommands );
            }
            else
            {
                $this->aCommands=array_merge( $this->aCommands, $mCommands );
            }
        }
        else
        {
            //SkipDebug
            if ( !empty( $mCommands ) )
            {
                $objLanguageManager=&xajaxLanguageManager::getInstance();
                trigger_error( $objLanguageManager->getText( 'XJXRSP:LCERR:01' ), E_USER_ERROR );
            }
        //EndSkipDebug
        }
    }

    function openProgress( )
    {
        if ( !$this->comet || $this->openProgress )
            return;
				$this->openProgress=false;
        $this->script(
            "
try {
			$('<div id=\"CMSprogressWIN\"/>').xWindow(
					
				{
					title:'Laden...',
					width:350,
					height:80,
					draggable:false,
					resizable:false,
					maximizable:false,
					minimizable:false,
					content: '<div id=\"CMSprogressContainer\" style=\"margin:10px;height:22px;width:300px;background:#ccc;\"><div id=\"CMSprogressBAR\" style=\"padding:2px;height:20px;width:1px;background:#333;color:#fff;text-align:right;\">0%</div></div>'
				});
} catch(ex) {
alert(ex);
}		
		" );
    }

    function updateProgress( $percent )
    {
        if ( !$this->comet )
            return;

        $this->assign( "CMSprogressBAR", 'style.width', ( $percent * 3 ) . 'px' );
        $this->assign( "CMSprogressBAR", 'innerHTML', ($percent) . '%' );
    }

    function closeProgress( )
    {
        if ( !$this->comet )
            return;

        $this->script( "$('#CMSprogressWIN').xWindow('destroy');" );
    }
}
